package com.chenjinyan.back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chenjinyan.back.entity.LoginCount;
import com.chenjinyan.back.service.LoginCountService;
import com.chenjinyan.back.utils.ResultUtil;
import com.chenjinyan.back.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/home")
public class HomePageController {

    @Autowired
    LoginCountService loginCountService;

    @GetMapping("/loginCount")
    public Result getLoginCount() {
        Result result = ResultUtil.success();
        Integer loginCountToday = loginCountService.getLoginCountToday();
        Integer loginCountMonth = loginCountService.getLoginCountMothy();
        Integer activeCountToday = loginCountService.getActiveCountToday() + loginCountService.getActiveMessageCountToday();
        Integer activeCountMonth = loginCountService.getActiveCountMonth() + loginCountService.getActiveMessageCountMonth();
        LocalDateTime now = LocalDateTime.now();
        String month = String.valueOf(now.getMonthValue());
        if (month.length() == 1) {
            month = "0" + month;
        }
        String date = now.getYear() + "-" + month + "-" + now.getDayOfMonth();
        List<LoginCount> list = loginCountService.getLoginCountByHour(date);
        Map<String, Object> map = new HashMap<>();
        //本天用户登陆次数，登陆一次就+1
        map.put("loginCountToday", loginCountToday);
        //本月用户登陆次数，登陆一次就+1
        map.put("loginCountMonth", loginCountMonth);
        //今天上传失物次数+今天留言次数
        map.put("activeCountToday", activeCountToday);
        //本月上传失物次数+今天留言次数
        map.put("activeCountMonth", activeCountMonth);
        map.put("activeCountByHour", list);
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
        result.setData(object);
        return result;
    }

    @GetMapping("/loginCountToday")
    public Result getLoginCountToday() {
        Result result = ResultUtil.success();
        LocalDateTime now = LocalDateTime.now();
        String month = String.valueOf(now.getMonthValue());
        if (month.length() == 1) {
            month = "0" + month;
        }
        String date = now.getYear() + "-" + month + "-" + now.getDayOfMonth();
        List<LoginCount> list = loginCountService.getLoginCountByHour(date);
        Map<String, Object> map = new HashMap<>();
        map.put("loginCountByHour", list);
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
        result.setData(object);
        return result;
    }

    @GetMapping("/loginCountMonth")
    public Result getLoginCountMonth() {
        Result result = ResultUtil.success();
        LocalDateTime now = LocalDateTime.now();
        String month = String.valueOf(now.getMonthValue());
        if (month.length() == 1) {
            month = "0" + month;
        }
        String date = now.getYear() + "-" + month;
        List<LoginCount> list = loginCountService.getLoginCountByDay(date);
        Map<String, Object> map = new HashMap<>();
        map.put("loginCountByDay", list);
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
        result.setData(object);
        return result;
    }

    @GetMapping("/activeCountToday")
    public Result getActiveCountToday() {
        Result result = ResultUtil.success();
        LocalDateTime now = LocalDateTime.now();
        String month = String.valueOf(now.getMonthValue());
        if (month.length() == 1) {
            month = "0" + month;
        }
        String date = now.getYear() + "-" + month + "-" + now.getDayOfMonth();
        List<LoginCount> list = loginCountService.getActiveCountByHour(date);
        Map<String, Object> map = new HashMap<>();
        map.put("activeCountToday", list);
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
        result.setData(object);
        return result;
    }

    @GetMapping("/activeCountMonth")
    public Result getActiveCountMonth() {
        Result result = ResultUtil.success();
        LocalDateTime now = LocalDateTime.now();
        String month = String.valueOf(now.getMonthValue());
        if (month.length() == 1) {
            month = "0" + month;
        }
        String date = now.getYear() + "-" + month + "-" + now.getDayOfMonth();
        List<LoginCount> list = loginCountService.getActiveCountByDay(date);
        Map<String, Object> map = new HashMap<>();
        map.put("activeCountMonth", list);
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
        result.setData(object);
        return result;
    }
}